package com.activenetwork.util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Function;

public class WaitUtil {
	
	static final WebDriver driver = EnvContext.webDriverTL.get();
	static final Logger logger = LoggerFactory.getLogger(WaitUtil.class);
	private WebDriverWait wait = new WebDriverWait(driver, 10);
	private int miniSec = 0;
	
	public WaitUtil(){}
	
	public WaitUtil(int miniSec){
		this.miniSec = miniSec;
	}
	
	public void waiting(){
		try {
			Thread.sleep(miniSec);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * @param miniSec
	 */
	public void wait(int miniSec){
		try {
			Thread.sleep(miniSec);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * @param locator
	 * @return
	 */
	private Function<WebDriver,WebElement> presenceOfElementLocated(final By locator) {
	    return new Function<WebDriver, WebElement>() {
	        @Override
	        public WebElement apply(WebDriver driver) {
	            return driver.findElement(locator);
	        }
	    };
	}
	/**
	 * 
	 * @param by
	 */
	public void waitUntilExist(By by) {
		wait.until(presenceOfElementLocated(by));
	}

}
